package bsqxday11;

import java.util.Scanner;

public class Test {
    //买卖股票二
    //链接：https://www.nowcoder.com/practice/fbc5dad3e215457fb82a3ae688eb7281?tpId=230&tqId=39768&ru=/exam/oj
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] prices = new int[n];
        for(int i = 0;i < n;i++) {
            prices[i] = in.nextInt();
        }

        //定义一个dp数组  表名手里有股票和没股票的状态

        int[][] dp = new int[n][2];  //表示手里有没有股票
        //初始化
        dp[0][0] = 0;   //手里没股票的利润
        //手里有股票的利润
        dp[0][1] = -prices[0];


        for(int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i-1][0],dp[i-1][1] + prices[i]);
            dp[i][1] = Math.max(dp[i-1][1],dp[i-1][0] - prices[i]);
        }
        System.out.println(dp[n-1][0]);
    }
}
